package MyGreed;

import java.util.Arrays;

public class Leetcode1838 {

    public static int maxFrequency(int[] nums, int k) {
        Arrays.sort(nums);
        int len = nums.length;
        if(len <=1){
            return 1;
        }
        long[] pre = new long[len];
        int maxx = 1;
        for(int i=0; i<len; i++){
            pre[i] = (i==0 ? nums[0] : pre[i-1] + nums[i]);
        }

        int left = 0;
        for(int right=1; right<len; right++){
            // 是否可以
            long have = (left == 0 ? pre[right] : pre[right] - pre[left-1]);
            long all = (right - left + 1) * (long)nums[right];
            while (all - have > k){
                left ++;
                break;
            }

            maxx = Math.max(maxx, right - left +1);
            System.out.println(left + " " + right + " " + maxx);
        }
        return maxx;
    }


    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4,5};
        int i = maxFrequency(nums, 6);
        System.out.println(i);
    }
}
